package bar.dark.lee.alarm.common;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
import bar.dark.lee.alarm.R;
import bar.dark.lee.alarm.common.wheel.OnWheelChangedListener;
import bar.dark.lee.alarm.common.wheel.OnWheelClickedListener;
import bar.dark.lee.alarm.common.wheel.OnWheelScrollListener;
import bar.dark.lee.alarm.common.wheel.WheelView;
import bar.dark.lee.alarm.common.wheel.adapters.NumericWheelAdapter;

public class CustomTimePickerDialog extends AlertDialog.Builder {

	private Context context;
	
	// Time scrolled flag
	private boolean timeScrolled = false;
	
	private WheelView viewHour;
	private WheelView viewMin;
	
	private int setHour;
	private int setMin;
	/**
	 * type : 1-현재시간 2-시간내놔
	 * @param context
	 * @param type
	 */
	public CustomTimePickerDialog(Context context, int type, int hour, int min) {
		super(context);
		
		this.context = context;
		
		if (type == 1)
		{
			// set current time
			Calendar c = Calendar.getInstance();
			setHour = c.get(Calendar.HOUR_OF_DAY);
			setMin = c.get(Calendar.MINUTE);
			
			setWheelSetting();
		}
		else if (type == 2)
		{
			setHour = hour;
			setMin = min;
			
			setWheelSetting();
		}
		else
		{
			Toast.makeText(context, "오류", 0).show();
		}
	}
	
	public String getHour()
	{
		return viewHour.getCurrentItem()+"";
	}
	
	public String getMin()
	{
		String min = viewMin.getCurrentItem()+"";
		
		if (min.equals("0"))
		{
			min = min + "0";
		}
		
		return min;
	}
	
	private void setWheelSetting()
	{
		LayoutInflater li = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		View v = li.inflate(R.layout.custom_timepicker_dialog, null);
		
		setView(v);
		
		viewHour = (WheelView) v.findViewById(R.id.customTimepickerHour);
		viewHour.setViewAdapter(new NumericWheelAdapter(context, 0, 23));
		viewHour.setCyclic(true);
		
		viewMin = (WheelView) v.findViewById(R.id.customTimepickerMins);
		viewMin.setViewAdapter(new NumericWheelAdapter(context, 0, 59, "%02d"));
		viewMin.setCyclic(true);
		
		viewHour.setCurrentItem(setHour);
		viewMin.setCurrentItem(setMin);
	
		// add listeners
		addChangingListener(viewMin, "min");
		addChangingListener(viewHour, "hour");
	
		OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!timeScrolled) {
				}
			}
		};
		viewHour.addChangingListener(wheelListener);
		viewMin.addChangingListener(wheelListener);
		
		OnWheelClickedListener click = new OnWheelClickedListener() {
            public void onItemClicked(WheelView wheel, int itemIndex) {
                wheel.setCurrentItem(itemIndex, true);
            }
        };
        viewHour.addClickingListener(click);
        viewMin.addClickingListener(click);

		OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				timeScrolled = true;
			}
			public void onScrollingFinished(WheelView wheel) {
				timeScrolled = false;
			}
		};
		viewHour.addScrollingListener(scrollListener);
		viewMin.addScrollingListener(scrollListener);
		
	}
	
	/**
	 * Adds changing listener for wheel that updates the wheel label
	 * @param wheel the wheel
	 * @param label the wheel label
	 */
	private void addChangingListener(final WheelView wheel, final String label) {
		wheel.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				//wheel.setLabel(newValue != 1 ? label + "s" : label);
			}
		});
	}
}
